#include "SelectionBoxManager.h"
#include <osg/ComputeBoundsVisitor>
#include <osg/PositionAttitudeTransform>


SelectionBoxManager::SelectionBoxManager(class SceneManager* pSceneManager):
m_rSceneManager(pSceneManager)
{
	m_rSelectionBox = new SelectionBox;
}

SelectionBoxManager::~SelectionBoxManager(void)
{
}


void SelectionBoxManager::add(osg::Node *pNode)
{
	osg::ComputeBoundsVisitor v;
	pNode->accept(v);

	osg::PositionAttitudeTransform* pBox = new osg::PositionAttitudeTransform;
	pBox->addChild( m_rSelectionBox.get() );

	osg::Vec3 c = v.getBoundingBox().center();
	float xMax = v.getBoundingBox().xMax();
	float yMax = v.getBoundingBox().yMax();
	float zMax = v.getBoundingBox().zMax();
	pBox->setPosition( c );
	pBox->setScale( osg::Vec3(xMax - c.x(), yMax - c.y(), zMax - c.z()) );

	m_rSceneManager->addObject(SceneManager::Internal, pBox);
	m_SelectionBoxes[pNode] = pBox;
}


void SelectionBoxManager::remove(osg::Node *pNode)
{
	SelectionBoxMap::iterator it = m_SelectionBoxes.find(pNode);
	if( it != m_SelectionBoxes.end() )
	{
		osg::Node* pBox = it->second.get();
		m_rSceneManager->removeObject(SceneManager::Internal, pBox);
		m_SelectionBoxes.erase(it);
	}
}